package com.har.modules.web.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.har.common.exception.RRException;
import com.har.common.utils.PageUtils;
import com.har.common.utils.Query;
import com.har.common.utils.StringUtil;
import com.har.modules.web.dao.PremiumRuleDao;
import com.har.modules.web.entity.PremiumRuleEntity;
import com.har.modules.web.service.PremiumRuleService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("premiumRuleService")
public class PremiumRuleServiceImpl extends ServiceImpl<PremiumRuleDao, PremiumRuleEntity> implements PremiumRuleService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<PremiumRuleEntity> page = this.selectPage(
                new Query<PremiumRuleEntity>(params).getPage(),
                new EntityWrapper<PremiumRuleEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PremiumRuleEntity queryRule(Map<String, Object> params) {
        EntityWrapper<PremiumRuleEntity> ew=new EntityWrapper<>();
        if(!StringUtil.isEmpty(params.get("type").toString())){
            ew.eq("type",params.get("type"));
        }
        if(!StringUtil.isEmpty((String) params.get("socialSecurity"))){
            ew.eq("social_security",params.get("socialSecurity"));
        }
        if (!StringUtil.isEmpty((String) params.get("rank"))){
            ew.eq("rank",params.get("rank"));
        }
        if(!StringUtil.isEmpty((String) params.get("ageBegin"))){
            ew.le("age_begin",params.get("ageBegin"));
        }
        if (!StringUtil.isEmpty((String) params.get("ageEnd"))){
            ew.eq("age_end",params.get("ageEnd"));
        }
        PremiumRuleEntity premiumRuleEntity=this.selectOne(ew);
        if(null==premiumRuleEntity){
            throw new RRException("不予投保");
        }
        premiumRuleEntity.setId(null);
        premiumRuleEntity.setAgeBegin(null);
        premiumRuleEntity.setCategory(null);
        premiumRuleEntity.setRemark(null);
        premiumRuleEntity.setAgeEnd(null);
        premiumRuleEntity.setOption(null);
        premiumRuleEntity.setRank(null);
        premiumRuleEntity.setSocialSecurity(null);
        premiumRuleEntity.setType(null);
        return premiumRuleEntity;
    }

}
